package zozopo.background.operator.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import zozopo.database.BusiDatabaseManager;
import zozopo.operations.comm.OperatingService;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.BackreceiptlistTB;
import zozopo.warehouse.operations.GoodsTB;


public class LimitedOS  extends OperatingService{
	
	
	public LimitedOS(Connection conn){
		super(conn);
	}
	
	public LimitedOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<LimitedTB> selectListBySql(String sql){
		List<LimitedTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		//Flow flow = new Flow(conn);
		//Map flowMap = flow.getFlowStateMapByKind(FlowCode.HOUSEWARE_RECEIPTLIST);
		if(resultSet != null){
				try {
					list = new ArrayList<LimitedTB>();
					while(resultSet.next()){
						LimitedTB obj = new LimitedTB();
						
						obj.setId_limited(resultSet.getInt(LimitedTB.ID_LIMITED));

						obj.setCode_limited(resultSet.getString(LimitedTB.CODE_LIMITED));
						
						obj.setKind_limited(resultSet.getString(LimitedTB.KIND_LIMITED));
						obj.setMenulevel_limited(resultSet.getInt(LimitedTB.MENULEVE_LIMITED));
						obj.setName_limited(resultSet.getString(LimitedTB.NAME_LIMITED));
						obj.setRemark_limited(resultSet.getString(LimitedTB.REMARK_LIMITED));
						obj.setUrl_limited(resultSet.getString(LimitedTB.URL_LIMITED));
						
						obj.setMainname_limited(resultSet.getString(LimitedTB.MAINNAME_LIMITED));
						obj.setMainurl_limited(resultSet.getString(LimitedTB.MAINURL_LIMITED));
						
						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<LimitedTB> selectAll(){
		String sql = "SELECT * FROM " + LimitedTB.TABLE_NAME;
		List<LimitedTB> list =  selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<LimitedTB> selectByUserLimitelist(List<OperatorlimitedTB> userlimitedList){

		Map<String,LimitedTB> userlimitedMap = selectMap();
		
		for(OperatorlimitedTB userlimited:userlimitedList){
			if(userlimitedMap.get(userlimited.getLimitedcode_operatorlimited()) != null){
				LimitedTB limited = userlimitedMap.get(userlimited.getLimitedcode_operatorlimited());
				limited.setChecked(true);
				userlimitedMap.put(limited.getCode_limited(), limited);	
			}
		}
		
		List<LimitedTB> list =  new ArrayList<LimitedTB>();
		for(LimitedTB limited:userlimitedMap.values()){
			
			list.add(limited);
		}
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<String,LimitedTB> selectMap(){
		
		String sql = "SELECT * FROM " + LimitedTB.TABLE_NAME;
		
		Map<String,LimitedTB> limitedMap = new LinkedHashMap<String,LimitedTB>();
		List<LimitedTB> limitedList =  selectListBySql(sql);
		
		
		for(LimitedTB limited:limitedList){
			limitedMap.put(limited.getCode_limited(), limited);

		}
		return limitedMap;
	}
	
	public LimitedTB get(int id){
		String sql = "SELECT * FROM " + LimitedTB.TABLE_NAME + " where " + LimitedTB.ID_LIMITED + "=" + id;
		LimitedTB obj = new LimitedTB();
		List<LimitedTB> list = selectListBySql(sql);
		obj = list.get(0); 
		return obj;
	}
	
	public LimitedTB getLimitedByCode(String code){
		String sql = "SELECT * FROM " + LimitedTB.TABLE_NAME + " where " + LimitedTB.CODE_LIMITED + "='" + code+"'";
		LimitedTB obj = new LimitedTB();
		List<LimitedTB> list = selectListBySql(sql);
		obj = list.get(0); 
		return obj;
	}
	
	public List<LimitedTB> selectAll(int beginPageNum,int pageSize){
		
		List<LimitedTB> list = null;
		String sql = "SELECT * FROM " + LimitedTB.TABLE_NAME  
		+ " ORDER BY " + LimitedTB.ID_LIMITED + " DESC "
		+ " limit "+beginPageNum+","+pageSize; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + LimitedTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<LimitedTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+LimitedTB.TABLE_NAME+" WHERE  "
		
		+" OR "+LimitedTB.ID_LIMITED+" LIKE '%"+searchStr+"%'"
		+" OR "+LimitedTB.NAME_LIMITED+" LIKE '%"+searchStr+"%'"
		+" OR "+LimitedTB.REMARK_LIMITED+" LIKE '%"+searchStr+"%'"
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectListBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + LimitedTB.TABLE_NAME +" WHERE  "
		+" OR "+LimitedTB.ID_LIMITED+" LIKE '%"+searchStr+"%'"
		+" OR "+LimitedTB.NAME_LIMITED+" LIKE '%"+searchStr+"%'"
		+" OR "+LimitedTB.REMARK_LIMITED+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	public  List<LimitedTB> getLimitedList(Map<String,OperatorlimitedTB> map){
		
		List<LimitedTB> limitedList = new ArrayList<LimitedTB>();
		List<LimitedTB> allLimitedList = selectAll();
		
			for(LimitedTB limited:allLimitedList){ 
				if(map.get(limited.getCode_limited()) != null){
					limitedList.add(limited);
				}
			}

		
		return limitedList;
		
	}
	
	public Map<String,LimitedTB> selectLimitedMap(){
		
		Map<String,LimitedTB> limitedMap = new HashMap<String,LimitedTB>();
		List<LimitedTB> allLimitedList = selectAll();
		
		for(LimitedTB limited:allLimitedList){ 
			
			limitedMap.put(limited.getCode_limited(), limited);
			
		}
		
		return limitedMap;
	}
}
